package com.qianchang.microfinance.view.register;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.bean.AgreementBean;
import com.qianchang.microfinance.bean.DataBean;
import com.qianchang.microfinance.bean.event.AgreementTitleBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import org.json.JSONArray;

import java.util.List;


public class AgreementApi {

    private static final String TAG = "AgreementApi";

    public static void getAgreementName(final BaseActivity context, JSONArray ids, final AgreementNameCallBack callBack) {
        context.show();
        HttpParams params = new HttpParams();
        params.put("ids", ids);
        DMOKHttpUtils.getInstance().post(context, DMConstant.ApiURL.FIND_TGREEMENT_NAME, params, new RequestCallBack<List<AgreementTitleBean>>() {
            @Override
            public void onSuccess(JsonResult<List<AgreementTitleBean>> response) {
                context.dismiss();
                if (response.isReqDataSuccess()) {
                    List<AgreementTitleBean> agreementTitleList = response.getObject();
                    if (null != callBack) {
                        callBack.success(agreementTitleList);
                    }
                } else {
                    DMLog.e(TAG, "onSuccess: " + response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                context.dismiss();
                DMLog.e(TAG, "onFailure: " + msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }


    public static void getInfo(final BaseActivity activity, String id, final CallBack callBack) {
        if (null == id) {
            return;
        }
//        0 现金贷借款协议
//        1 消费贷借款协议
//        2 抵押贷借款协议
//        3 租房分期借款协议
//        4 学生贷借款协议
//        5 业主贷借款协议
//        6 用户服务协议
//        7 个人征信查询授权书
//        8 委托扣款授权书
//        9 数字证书服务协议
//        10 第三方风控服务授权书
        activity.show();
        HttpParams params = new HttpParams();
        params.put("id", id);
        DMOKHttpUtils.getInstance().post(activity, DMConstant.ApiURL.FIND_AGREEMENT_COMMON, params, new RequestCallBack<DataBean<AgreementBean>>() {
            @Override
            public void onSuccess(JsonResult<DataBean<AgreementBean>> response) {
                activity.dismiss();
                DataBean<AgreementBean> entityList = response.getObject();
                if (entityList != null && entityList.getList() != null && !entityList.getList().isEmpty()) {
                    callBack.success(response.getObject().getList().get(0));
                } else {
                    activity.showDataError();
                }
            }

            @Override
            public void onFailure(String msg) {
                activity.dismiss();
                ToastUtil.getInstant().show(activity, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    public interface CallBack {
        void success(AgreementBean agreementBean);
    }

    public interface AgreementNameCallBack {
        void success(List<AgreementTitleBean> agreementName);
    }


}
